﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Xml.Linq;

namespace Shiny.SmartTravel.Data.Local
{
    /// <summary>
    /// Weather info
    /// </summary>
    public class Weather : INotifyPropertyChanged
    {
        /// <summary>
        /// Initialize
        /// </summary>
        /// <param name="weatherXml"></param>
        public Weather(string weatherXml)
        {
            XDocument doc = XDocument.Parse(weatherXml, LoadOptions.PreserveWhitespace);
            this.Location = (string)doc.Element("CurrentWeather").Element("Location");
            this.PubTime = (string)doc.Element("CurrentWeather").Element("Time");
            this.Wind = (string)doc.Element("CurrentWeather").Element("Wind");
            this.Visibility = (string)doc.Element("CurrentWeather").Element("Visibility");
            this.skyConditions = (string)doc.Element("CurrentWeather").Element("SkyConditions");
            this.Temperature = (string)doc.Element("CurrentWeather").Element("Temperature");
            this.DewPoint = (string)doc.Element("CurrentWeather").Element("DewPoint");
            this.RelativeHummidity = (string)doc.Element("CurrentWeather").Element("RelativeHumidity");
            this.Pressure = (string)doc.Element("CurrentWeather").Element("Pressure");
            this.Success = (string)doc.Element("CurrentWeather").Element("Status") == "Success";
        }

        /// <summary>
        /// Gets or sets the location
        /// </summary>
        public string Location { get; set; }

        /// <summary>
        /// Gets or sets the publish time 
        /// </summary>
        public string PubTime { get; set; }

        /// <summary>
        /// Gets or sets the wind
        /// </summary>
        public string Wind { get; set; }

        /// <summary>
        /// Gets or sets the visibility
        /// </summary>
        public string Visibility { get; set; }

        private string skyConditions;
        /// <summary>
        /// Gets or sets the sky conditions
        /// </summary>
        public string SkyConditions
        {
            get { return skyConditions; }
            set
            {
                skyConditions = value;
                OnPropertyChanged("SkyConditions");
            }
        }

        /// <summary>
        /// Gets or sets the temperature
        /// </summary>
        public string Temperature { get; set; }

        /// <summary>
        /// Gets or sets the dewpoint
        /// </summary>
        public string DewPoint { get; set; }

        /// <summary>
        /// Gets or sets the relative hummidity
        /// </summary>
        public string RelativeHummidity { get; set; }

        /// <summary>
        /// Gets or sets the pressure
        /// </summary>
        public string Pressure { get; set; }

        /// <summary>
        /// Gets or sets the status
        /// </summary>
        public bool Success { get; set; }

        #region INotifyPropertyChanged Members

        /// <summary>
        /// Notify
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        /// <summary>
        /// Notify
        /// </summary>
        /// <param name="name"></param>
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(name));
        }

        /// <summary>
        /// Returns the SkyConditions
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return skyConditions;
        }
    }
}
